package com.example.conference.utils;

import lombok.Data;

import java.util.HashMap;

/**
 * 封装通用返回类
 */
@Data
public class Result {
    private static final Integer SUCCESS_CODE = 20000;
    private static final Integer ERROR_CODE = 20001;

    private Integer code;//code：接口的响应结果
    private String message;//message：存放错误信息
    private HashMap<String, Object> data = new HashMap<>();
    //无参数的成功方法，只返回成功代码“200”
    public static Result success() {
        Result result = new Result();
        result.setCode(SUCCESS_CODE);
        return result;
    }

    //有参数的成功方法，返回成功代码“200” 和 数据data
    public static Result success(Object data) {
        Result result = new Result();
        result.setCode(SUCCESS_CODE);
        return result;
    }
    public Result data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }
    //失败的方法，返回自定义错误信息 和 错误代码“-1”
    public static Result error(String msg) {
        Result result = new Result();
        result.setCode(ERROR_CODE);
        result.setMessage(msg);
        return result;
    }

    //失败的方法，返回自定义错误信息 和 自定义错误代码
    public static Result error(Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }

}
